﻿
//namespace GBToUnicode
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//        }
//    }
//}


using System;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.InteropServices;
//using coding;


namespace GBToUnicode
{

    class Program
    {
        //[DllImport("coding.dll", CallingConvention = CallingConvention.StdCall)]
        //[DllImport(@"coding.dll",CallingConvention = CallingConvention.StdCall)]
        //[DllImport(@"coding.dll")]
       // static extern void testcall();
        //static extern wchar_t* ConvertUtf8ToUnicode(char* putf8);

//istr:查找字符串,startString:开始字符串,endString:结束字符串 
       public static string mid(string istr, string startString, string endString) 
       { 
           int iBodyStart = istr.IndexOf(startString,0);               //开始位置 
           
           iBodyStart += startString.Length;                           //第一次字符位置起的长度
           int iBodyEnd = istr.IndexOf(endString, iBodyStart);         //第二次字符在第一次字符位置起的首次位置 
           iBodyEnd += endString.Length;                              //第二次字符位置起的长度 
           if(iBodyStart==0 || iBodyEnd==0)
           {
           		return "";
           }
           string strResult = istr.Substring(iBodyStart, iBodyEnd - iBodyStart-endString.Length);
           return strResult; 
       } 

//本文来自网学(http://myeducs.cn)，转载请注明出处：http://myeducs.cn/lunwen-resource/Net-biancheng/C-chazhaozifuchuanjiequhanshu/
        /// <summary> 
        /// 将汉字转换为Unicode 
        /// </summary> 
        /// <param name="strGB">要转换的字符串</param> 
        /// <returns></returns> 
        public static string GBToUnicode(string strGB,string type="")
        {
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(strGB);
            string lowCode = "", tmpChar = "";
            if(type!="")
            {
            	lowCode = lowCode + "\t(" + type +")\"";
            }else
            {
            	lowCode = lowCode + "\t\"";
            }

            //Console.WriteLine("unicode bytes.Length=={0}", bytes.Length);
            for (int i = 0; i < bytes.Length; i++)
            {
                //if (i % 2 == 0)
                //{
                //    tmpChar = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容（两位16进制） 
                //    if (tmpChar.Length < 2)
                //        tmpChar = "0" + tmpChar;
                //}
                //else
                //{
                //    string mytemp = Convert.ToString(bytes[i], 16);
                //    if (mytemp.Length < 2)
                //        mytemp = "0" + mytemp;
                //    lowCode = lowCode + @"\u" + mytemp + tmpChar;//取出元素4编码内容（两位16进制） 
                //}
                tmpChar = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容（两位16进制） 
                if (tmpChar.Length < 2)
                    tmpChar = "0" + tmpChar;
                {
                	lowCode = lowCode + @"\x" + tmpChar;//取出元素4编码内容（两位16进制）
                }
                
            }
            lowCode = lowCode + "\",";
            return lowCode;
        }

        public static string GBToUtf8(string strGB,string type="")
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strGB);
            string lowCode = "", tmpChar = "";
            if (type != "")
            {
                lowCode = lowCode + "\t(" + type + ")\"";
            }
            else
            {
                lowCode = lowCode + "\t\"";
            }

            //Console.WriteLine("utf8  bytes.Length=={0}", bytes.Length);
            for (int i = 0; i < bytes.Length; i++)
            {
                tmpChar = System.Convert.ToString(bytes[i],16);//取出元素4编码内容（两位16进制） 
                if (tmpChar.Length <2)
                    tmpChar = "0" + tmpChar;
                {
                    lowCode = lowCode + @"\x" + tmpChar;//取出元素4编码内容（两位16进制）
                }

            }
            lowCode = lowCode + "\",";
            return lowCode;
        }
        /// <summary> 
        /// 将Unicode转换为汉字 
        /// </summary> 
        /// <param name="name">要转换的字符串</param> 
        /// <returns></returns> 
        public string UnicodeToGB(string text)
        {

            //MatchCollection mc = Regex.Matches(text, "([\\w]+)|(\\\\u([\\w]{4}))");
            //if (mc != null && mc.Count > 0)
            //{
            //    StringBuilder sb = new StringBuilder();
            //    foreach (Match m2 in mc)
            //    {
            //        string v = m2.Value;
            //        string word = v.Substring(2);
            //        byte[] codes = new byte[2];
            //        int code = Convert.ToInt32(word.Substring(0, 2), 16);
            //        int code2 = Convert.ToInt32(word.Substring(2), 16);
            //        codes[0] = (byte)code2;
            //        codes[1] = (byte)code;
            //        sb.Append(Encoding.Unicode.GetString(codes));
            //    }
            //    return sb.ToString();
            //}
            //else
            {
                return text;
            }
        }


        static void Main(string[] args)
        {
        	string type="";
        	if(args.Length>=3)
        	{
        		type=args[2];
        	}
        	Console.WriteLine("{0}",args[2]);
            string test = null;


            StreamReader din = new StreamReader(args[0], System.Text.Encoding.GetEncoding("gb2312"));

			//8.写入文件
			//using System.IO;
			FileInfo f_unicode = new FileInfo(args[1]+"_unicode.txt");
            StreamWriter w_unicode = f_unicode.CreateText();
            FileInfo f_utf8 = new FileInfo(args[1]+"_utf8.txt");
            StreamWriter w_utf8 = f_utf8.CreateText();
			//w.WriteLine("{");
//           	while (din.Peek() > -1)
//            {
//                test = test + din.ReadToEnd();
//            }
//           	for(int i=0;i<test.Length;i++)
//           	{
//           		Console.WriteLine("{0}",(int)test[i]);
//           	}

            int count=0;
            while ((test = din.ReadLine()) != null)
            {
            	count++;
            	if(test=="")
            	{
            		continue;
            	}else if((test=="{") || (test=="}") || (test==",") || (test=="},"))
            	{
            		Console.WriteLine("{0}  {1}",count, test);
            		w_unicode.WriteLine(test);
                    w_utf8.WriteLine(test);
            		continue;
            	}
                
            	if(mid(test.Trim(),"\"","\"")!="")
            	{
                    //unicode
            		Console.WriteLine("{0}  {1}",count, mid(test.Trim(),"\"","\""));
            		Console.WriteLine("{0}  {1}",count, GBToUnicode(mid(test.Trim(),"\"","\"")));
                    w_unicode.WriteLine("{0} {1} {2} {3}", "\t//", count, "、", mid(test.Trim(), "\"", "\""));
                    w_unicode.WriteLine( GBToUnicode(mid(test.Trim(), "\"", "\""), type));
                    //utf-8
                    w_utf8.WriteLine("{0} {1} {2} {3}", "\t//", count, "、", mid(test.Trim(), "\"", "\""));
                    w_utf8.WriteLine(GBToUtf8(mid(test.Trim(), "\"", "\""), type));
            	}else
            	{
                    //unicode 
            		Console.WriteLine("{0}  {1}",count, test.Trim());
            		Console.WriteLine("{0}  {1}",count, GBToUnicode(test.Trim()));
                    
                    w_unicode.WriteLine("{0} {1} {2} {3}", "\t//", count, "、", test.Trim());
                    w_unicode.WriteLine(GBToUnicode(test.Trim(), type));
                    //utf-8
                    Console.WriteLine("{0}  Utf-8::{1}", count, GBToUtf8(test.Trim()));
                    w_utf8.WriteLine("{0} {1} {2} {3}", "\t//", count, "、", test.Trim());
                    w_utf8.WriteLine(GBToUtf8(test.Trim(), type));
            	}
            }
 			//w.WriteLine("}");
            din.Close();
            //char[] ascill = new char[128];
            //for (int i = 0; i < 128; i++)
            //{
            //    ascill[i] = (char)i;
            //}
            //string str_asci = new string(ascill);

            //Console.WriteLine("{0} ", GBToUnicode(str_asci));
            //w.WriteLine(GBToUnicode(str_asci, type));

			w_unicode.Flush();
			w_unicode.Close();
            w_utf8.Flush();
            w_utf8.Close();
            //Console.ReadKey(true);
            //byte[] uc = Encoding.Unicode.GetBytes();
        }
    }
}

//用c#读取文件内容中文是乱码的解决方法  //方法1： 
//            StreamReader din = new StreamReader(@"C:\1.txt", System.Text.Encoding.GetEncoding("gb2312"));
//            string html = "";
//            while (din.Peek() > -1)
//            {
//                html = html + din.ReadToEnd();
//            }
//            din.Close();

//            //方法2：
//            StreamReader sr1 = new StreamReader((System.IO.Stream)File.OpenRead(filename), System.Text.Encoding.Default);
//            html = "";
//            while (sr1.Peek() > -1)
//            {
//                html = html + sr1.ReadLine();
//            }
//            sr1.Close();

//            //方法3：
//            StreamReader objReader = new StreamReader(@"C:\1.txt", System.Text.Encoding.GetEncoding("gb2312"));
//            string sLine = "", html = "";
//            while (sLine != null)
//            {
//                sLine = objReader.ReadLine();
//                if (sLine != null)
//                    html += sLine;
//            }
//            objReader.Close();
